package com.customization.huangwm.cronJob.executeActions;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.customization.huangwm.utils.LogTool;
import org.apache.axis.components.logger.LogFactory;
import org.apache.commons.logging.Log;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.formmode.setup.ModeRightInfo;
import c.u.d.c.jc;

import com.customization.huangwm.cronJob.AbstractCronJob;

/**
 * @author Huangwm
 * @package com.customization.huangwm.cronJob.executeActions
 * @description 从合约列表中获取稽查人员更新至对应的项目工程
 */
public class UpdateInspectorsToProject extends AbstractCronJob {
    private LogTool log = new LogTool("/log/huangwm/cronJob/UpdateInspectorsToProject", false);
    public String modeid = "";

    public String getmodeid() {
        return modeid;
    }

    public void setmodeid(String modeid) {
        this.modeid = modeid;
    }

    /*
     * 1.先获取当前有多少工程项目
     * 2.查询合约表里当前循环的工程项目对应的记录，并且获取出稽查人员
     * 3.将汇总的稽查人员更新至工程项目的稽查人员字段
     */
    @Override
    public void start() throws Exception {
        log.writeLog("UpdateInspectorsToProject计划任务开始");
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();

        //循环工程项目表
        rs1.execute("select id,Inspector,modedatacreater from uf_xmproject");
        while (rs1.next()) {
            String id = rs1.getString("id");
            Integer modedatacreater = rs1.getInt("modedatacreater");
            rs2.execute("select id,AssociatedProjectName ,ContractInspector from uf_project  where gcmc=" + id);
            StringBuffer sb = new StringBuffer();
            List<String> jcryList = new ArrayList<String>();
            while (rs2.next()) {
                String jcry2 = rs2.getString("ContractInspector");
                sb = sb.append(jcry2 + ",");
                if (!jcryList.contains(jcry2)) {
                    jcryList.add(jcry2);
                }
            }
            //String up = "update uf_xmproject set jcry='"+sb.toString().substring(0, sb.toString().length()-1)+"' where id="+id;
            String up = "update uf_xmproject set Inspector='" + jcryList.stream().collect(Collectors.joining(",")) + "' where id=" + id;
            boolean result = rs3.executeUpdate(up);
            log.writeLog(up + ">>>" + result);
            ModeRightInfo right = new ModeRightInfo();
            right.editModeDataShare(modedatacreater, Integer.parseInt(modeid), Integer.parseInt(id));
        }
        log.writeLog("UpdateInspectorsToProject计划任务结束");
    }
}
